
use "C:/Users/awcassidy1\Dropbox\jmp_new\cleaned_data/audit_3.dta", clear

ds _all
return list
local varlist=r(varlist)




merge 1:m delivery_point_barcode using "C:/Users/awcassidy1\Dropbox\jmp_new\cleaned_data/data_used_in_main_regs.dta"
*make sure none are just in using.
assert _merge!=2


sum _merge

cap drop *_st

gen main_sample=0
replace main_sample = 1 if _merge==3


local less_observable_vars="atticrvalue ductravg duct_type negleakavg rsavg  eeravg"
local more_observable_vars="ah_location sizeavg progtherm fftype whftype negsysageavg"
local other_less_observable_vars="ah_type notwinrec"
local other_more_observable_vars="twosystems notraddr"

cap gen fftype=furnacefueltype
replace fftype=furnacefueltype if missing(fftype)

cap gen whftype=waterheaterfueltype
replace whftype=waterheaterfueltype if missing(whftype)

cap gen ah_location=system1locationairhandler
replace ah_location=system1locationairhandler if missing(ah_location)

cap gen ah_type=system1airhandlertype
replace ah_type=system1airhandlertype if missing(ah_type)


cap gen duct_type=ductsystem1type
replace duct_type=ductsystem1type if missing(ah_type)

local parsimonious_less_vars "negleakavg atticrvalue ductravg     "
local parsimonious_more_vars_fuel "fftype whftype "
local parsimonious_more_vars_other "ah_location progtherm twosystems"

local allvars "`less_observable_vars' `more_observable_vars' `other_less_observable_vars' `other_more_observable_vars'"

sum main_sample

cap gen ah_location=system1locationairhandler
cap gen ah_type=system1airhandlertype
cap gen wh_tanktype=waterheatertanktype

cap rename ductsystem1type duct_type
cap rename ductsystem1type duct_type


sum notwinrec 
cap gen notwinrec_st=(notwinrec-r(mean))/r(sd)
replace notwinrec_st=(notwinrec-r(mean))/r(sd)

sum twosystems
cap gen twosystems_st=(twosystems-r(mean))/r(sd)
replace twosystems_st=(twosystems-r(mean))/r(sd)


cap gen notraddr=(rrec==0) if !missing(rrec) 
sum notraddr
cap gen notraddr_st=(notraddr-r(mean))/r(sd)

cap rename system1airhandlertype ah_type
cap rename system1locationairhandler ah_location
cap rename ductsystem1type_st duct_type_st

local list_to_standardize `allvars'

*some of these don't have the standardized values
foreach w in `list_to_standardize'  {
	sum `w' 
	*cap drop `w'_st
	cap gen  `w'_st=(`w'-r(mean))/(r(sd))
	replace `w'_st=(`w'-r(mean))/(r(sd)) if missing(`w'_st)
	la var `w'_st "`: var label `w''"
	}

cap drop _merge
	cap drop dum
	cap gen dum=1
	
	merge m:1 dum using "C:/Users/awcassidy1\Dropbox\jmp_new\cleaned_data/load_tt_from_test.dta"
	
	gen Less=0
	gen More_Fuel=0
	gen More_Other=0
	foreach v in `parsimonious_less_vars' {
		replace Less = Less+ `v'_st*`v'_Less
	}
	foreach v in `parsimonious_more_vars_fuel' {
		replace More_Fuel = More_Fuel+ `v'_st*`v'_More_Fuel
	}
	foreach v in `parsimonious_more_vars_other' {
		replace More_Other = More_Other+ `v'_st*`v'_More_Other
	}

sum Less, detail	
gen above_median_less=(Less>r(p50)) if !missing(Less)
tab above_median_less

la var Less "Less"
la var More_Other "More-Other"
la var More_Fuel "More-Fuel"

la var wh_tanktype "Tankless or Solar WH"

la var notraddr "Did not Rec Add Attic R"
la var negleakavg "$-$ \% Leakage"
la var atticrvalue "Attic R-value"


la var negleakavg "$-$ \% Duct Leakage"

la var duct_type "Metal Ducts"
la var twosystems "2 Systems"
la var fftype "Gas Furnace"
la var ah_type "Vertical AH"
la var whftype "Gas WH"
la var ah_location "AH in Closet"

la var eeravg "EER"
la var negleakavg "$-$ \% Duct Leakage"
la var atticrvalue "Attic R-value"

la var rsavg "Return Sizing Adequate"
la var ductravg "Duct R-value"

la var negsysageavg "$-$ System Age"

la var sizeavg "HVAC Size (sqft/ton)"
la var notwinrec "Did Not Rec WS"

la var progtherm "Programmable Thermostat"
la var rrec "Amount of Recommended Additional R-value"
la var notraddr "Did Not Rec Att R"

local list_to_standardize Less More_Fuel More_Other 



foreach w in `list_to_standardize'  {
	sum `w' 
	gen `w'_st=(`w'-r(mean))/(r(sd))
	la var `w'_st "`: var label `w''"
	}


gen other_audited=1 if main_sample==0
replace other_audited=0 if main_sample==1
sum other_audited


preserve
*test to see if there was a big difference in the audits not in my sample.
eststo clear




eststo: estpost ttest    ///
	 Less_st ///
	 More_Fuel ///
	 More_Other ///
	 `allvars' ///
	  ///
	 , by(other_audited) unequal

esttab using "C:\Users/awcassidy1/Dropbox\jmp_new\tables/bal_repeat_sale_vs_not.tex", ///
				 cells("mu_1(fmt(3)) mu_2(fmt(3)) b(star fmt(4)) p(fmt(3))  N_1(fmt(%9.3gc)) N_2(fmt(%9.3gc))") ///
				replace fragment noobs ///
				label booktabs   ///
				collabels(none) nonumber ///
				 unstack wide ///
				 nolines gaps ///
				 star(* 0.10 ** 0.05 *** 0.01)
restore

set scheme plotplainblind



preserve
	twoway (kdensity Less if  main_sample==1, bwidth(.25)) || (kdensity Less if main_sample==0 , bwidth(.25)), ///
		ytitle("Kernel Density") ///
		xtitle("Less") ///
		legend(label(1 "Main Sample") label( 2 "Other Audited Homes"))
	graph export "C:\Users/awcassidy1/Dropbox\jmp_new\figs/Less_kdensity_main_vs_other_audited.pdf", replace as(pdf)

	twoway (kdensity More_Fuel if  main_sample==1,  bwidth(.5)) || (kdensity More_Fuel if main_sample==0, bwidth(.5)) , ///
		ytitle("Kernel Density") ///
		xtitle("More-Fuel") ///
		legend(label(1 "Main Sample") label( 2 "Other Audited Homes"))
	graph export "C:\Users/awcassidy1/Dropbox\jmp_new\figs/More_Fuel_kdensity_main_vs_other_audited.pdf", replace as(pdf)


	twoway (kdensity More_Other if  main_sample==1, bwidth(.5)) || (kdensity More_Other if main_sample==0, bwidth(.5)) , ///
		ytitle("Kernel Density") ///
		xtitle("More-Other") ///
		legend(label(1 "Main Sample") label( 2 "Other Audited Homes"))
	graph export "C:\Users/awcassidy1/Dropbox\jmp_new\figs/More_Other_kdensity_main_vs_other_audited.pdf", replace as(pdf)

restore


local list_to_compare Less_st ///
	 More_Fuel ///
	 More_Other ///
	 `allvars'


	 
stddiff  `list_to_compare' , ///
	 by(other_audited)
	 


local words= `: word count `list_to_compare''

mat output=r(output)

mat c_1=output[1..`words',5]

mat li c_1

*second col is variance ratios.
*initialize it.
mat c_2=c_1


forval i=1/`words' {
	mat c_2[`i',1] = (output[`i',2])^2/(output[`i',4])^2
	}
mat li c_1
mat li c_2

mat R=[c_1, c_2]
mat li R

ereturn list

eststo clear

estadd matrix R



esttab e(R, fmt(%9.3fc)) using "C:\Users\awcassidy1\Dropbox\jmp_new/tables/in_sample_vs_otheraudited_std_diffs.tex", ///
	label nodepvar replace coeflabels(`e(labels)') nomtitles ///
	collabels(none) ///
	b(%9.3fc) ///
	refcat(Less_st  " \addlinespace \textbf{EE Indexes:}" atticrvalue " \addlinespace \textbf{Audit Features:}", nolabel) ///
	mlabels(none) eqlabels(none) booktabs fragment
	
